package com.mju.controller;

import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Cart;
import com.mju.service.ICartService;
import com.mju.vo.CartVO;
import com.netflix.ribbon.proxy.annotation.Http;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/carts")
@Api(tags = "购物车服务")
public class CartController extends BaseController {
    @Autowired
    private ICartService cartService;

    @ApiOperation("添加到购物车")
    @PostMapping("/add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");
        cartService.addToCart(
                Integer.valueOf(uid),
                pid,
                amount,
                uname
        );
        return new JsonResult<Void>(OK);
    }

    @ApiOperation("根据用户id查询购物车")
    @GetMapping
    public JsonResult<List<CartVO>> getVOByUid(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        List<CartVO> data = cartService.getVOByUid(Integer.valueOf(uid));
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("添加购物车内商品个数")
    @GetMapping("/{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");

        Integer data = cartService.addNum(cid, Integer.parseInt(uid), uname);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("减少购物车的商品数量")
    @GetMapping("/{cid}/num/sub")
    public JsonResult<Integer> subNum(@PathVariable("cid") Integer cid, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uid = map.get("uid");
        String uname = map.get("uname");

        Integer data = cartService.subNum(cid, Integer.parseInt(uid), uname);
        return new JsonResult<>(OK, data);
    }


    @ApiOperation("查询购物车")
    @GetMapping("/list")
    public JsonResult<List<CartVO>> selectVOByCids(Integer[] cids, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        List<CartVO> data = cartService.getVOByCids(Integer.valueOf(uid), cids);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("根据用户id查询购物车")
    @GetMapping("/list/{uid}")
    public JsonResult<List<CartVO>> selectVOByUidAndCids(@PathVariable("uid")Integer uid,
                                                         @RequestParam("cids") Integer[] cids) {
        List<CartVO> data = cartService.getVOByCids(uid, cids);
        return new JsonResult<>(OK, data);
    }

    //处理根据cids内的指定cid删除cart的请求
    @ApiOperation("根据cids内的指定cid删除购物车")
    @GetMapping("/deleteCart")
    public JsonResult<Void> deleteCartByCid(Integer[] cids){
        //遍历执行删除操作
        for (Integer cid: cids) {
            cartService.deleteCartByCid(cid);
        }
        return new JsonResult<>(OK);
    }

    // 获取购物车的详情信息(获取一条数据)
    @ApiOperation("获取购物车的详情信息")
    @GetMapping("/queryCart/{cid}")
    public JsonResult<Cart> queryCartByCid(@PathVariable("cid") Integer cid) {
        Cart data = cartService.queryCartByCid(cid);
        return new JsonResult<>(OK, data);
    }

    // 根据pid获取对应的cid
    @ApiOperation("根据商品id获取对应的购物车")
    @GetMapping("/queryCartByPid/{pid}")
    public JsonResult<Integer> queryCartByPid(HttpServletRequest request, @PathVariable Integer pid) {
        String token = request.getHeader("Authorization");
        String uid = parseTokenGetUid(token);
        Cart cart = cartService.queryCartByUidAndPid(Integer.parseInt(uid), pid);
        Integer data = cart.getCid();
        return new JsonResult<>(OK, data);
    }
}
